#!/bin/bash

#----------------------------------------------------
# File: array.sh
# Contents: bash 当中的数组操作
# Date: 19-3-20
#----------------------------------------------------


#---------------------------------------------------------------------------------------------------
# 数组声明和初始化:
#
# declare -a array=()  // 声明空数组
#
# declare -a array=(ele ele ele) // 声明并且初始化
#
# array=(ele ele ele) 或者 array=([1]=ele [2]=ele [0]=ele) // 初始化
#
#---------------------------------------------------------------------------------------------------


#---------------------------------------------------------------------------------------------------
# 数组长度:
#
# ${#array[@]} // 推荐使用
#
# ${#array[*]}
#
#---------------------------------------------------------------------------------------------------

#---------------------------------------------------------------------------------------------------
# 数组遍历:
#
# for i in ${array[@]}; do echo $i; done // 推荐使用
#
# for i in ${array[*]}; do echo $i; done
#
# for i in ${!array[@]}; do echo $array[$i]; done // 其中 $i 是索引
#
# ${array[@]} 和 ${array[*]} 区别:
#    ${array[@]}  和  ${array[*]} 是没有区别的.
#   "${array[@]}" 和 "${array[*]}" 有区别, "${array[@]}" 最大限度保持原意, 是一个数组;
#   "${array[*]}" 是一个字符串
#
#---------------------------------------------------------------------------------------------------

#---------------------------------------------------------------------------------------------------
# 数组清除:
#  unset array  // 清除数组
#
#  unset array[n] // 清除第n+1个元素
#---------------------------------------------------------------------------------------------------

#---------------------------------------------------------------------------------------------------
# 数组操作:
#
#  array=("${one[@]}" "${two[@]}")  // 数组连接操作
#
#  new=("${old[@]}") // 数组复制操作
#
#  array=("${array[@]}" "xx") // 数组添加元素
#
#  array="${array[@]:n:len}" // 从第n个位置开始, 提取len个元素, 数组的分割
#---------------------------------------------------------------------------------------------------

echo "========================================"
array=([1]=11 [2]=22 [0]=33 [3]=44 [4]='a            b')
echo "原始数组: ${array[@]}"
echo "原始数组: ${array[*]}"

echo "数组长度: ${#array[@]}"
echo "数组长度: ${#array[*]}"
echo

echo "========================================"
echo "数组遍历1: "
for i in "${array[@]}"
do
    echo "${i}"
done
echo

echo "========================================"
echo "数组遍历2: "
for i in "${array[*]}"
do
    echo "${i}"
done
echo

echo "========================================"
echo "数组遍历3: "
for i in ${!array[@]}
do
    echo "${i} -- ${array[$i]}"
done
echo

echo "========================================"
echo "数组操作: "

echo "数组截取: 1,3 ${array[*]:1:3}"